package com.weilun.controller;


import com.weilun.pojo.Dept;
import com.weilun.pojo.Result;
import com.weilun.service.DeptService;
import com.weilun.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList= deptService.findAll();
        return Result.success(deptList);
    }

    @DeleteMapping
    public Result delete(@RequestParam(value = "id",required = false) Integer id){
        //System.out.println("根据id删除员工："+id);
        log.info("根据id删除员工：{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    //@RequestBody: 将请求体中json格式的请求参数，封装到对应的对象中
    public Result add(@RequestBody Dept dept){
        //System.out.println("新增部门："+dept);
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    /*
    * 根据id查询部门，用于页面的回显数据
    * */
    @GetMapping("/{id}")
    //@PathVariable: 声明获取的是路径参数，方法形参要和路径参数名称保持一致
    public Result getById(@PathVariable Integer id){
        //System.out.println("根据id查询部门："+id);
        log.info("根据id查询部门：{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /*
    * 修改部门名称
    * */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门："+dept);
        log.info("修改部门：{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
